package com.eshop.we.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.eshop.we.exchange.api.ExchangeService;
import com.eshop.we.exchange.dto.ExchangeDto;
import com.eshop.we.exchange.entity.Exchange;
import com.eshop.we.exchange.enums.DelEnum;
import com.eshop.we.exchange.enums.StatusEnum;
import com.eshop.we.exchange.form.ExchangeForm;
import com.eshop.we.mapper.ExchangeDetailMapper;
import com.eshop.we.mapper.ExchangeMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * <p>
 * 换书信息 服务实现类
 * </p>
 *
 * @author anonymity
 * @since 2018-07-04
 */
@Service(interfaceClass = ExchangeService.class)
@Component
public class ExchangeServiceImpl extends ServiceImpl<ExchangeMapper, Exchange> implements ExchangeService {

    @Autowired
    ExchangeDetailMapper exchangeDetailMapper;

    @Override
    public List<ExchangeDto> selectExchangeList(Integer userId) {
        return baseMapper.selectExchangeList(userId);
    }

    @Override
    public Integer updateExchangeDel(Integer id) {
        exchangeDetailMapper.updateDel(id);//主表id
        Exchange exchange=new Exchange();
        exchange.setId(id);
        exchange.setIsDel(1);
        return    baseMapper.updateById(exchange);
    }

    @Override
    public List<ExchangeDto> selectMathExchange(Integer exchangeId,Integer schoolId, Integer userId) {
        //查询换书详情的书名list
     //    String bookList= StringUtils.join(exchangeDetailMapper.selectListBook(exchangeId), ",");
        return exchangeDetailMapper.selectMathBook(userId,schoolId,baseMapper.selectBookName(exchangeId));
    }

    @Override
    public void add(ExchangeForm exchangeForm) {
        Exchange exchange = new Exchange();
        BeanUtils.copyProperties(exchange,exchangeForm);
        exchange.setIsDel(DelEnum.IS_DEL_NO.getCode());
        exchange.setStatus(StatusEnum.STATUS_UP.getCode());
        exchange.setCtime(new Long(System.currentTimeMillis()/1000).intValue());
        baseMapper.insert(exchange);
        exchangeDetailMapper.batchInsert(exchange,exchangeForm.getDetails());
    }
}
